package com.example.segment_saver;
/*
 * Nov 16th 2012
 * Created By: Alex Drotar, Jacob Reiger, and Sean Egan
 * Using code from: Ken Anderson 
 * Segment_Saver- This is ment to be run on Andriod mobile devices
 * 					It was created for our Object Orientated Design and Analysis class
 * 					The program allows the user to watch specific parts of a site
 * 					The hopes of this project are to allow a user to say that the site
 * 					Is a blog and it will check to see if there are any new blogs or something of the sort 
 * 					To that site. If the user presses the top button Update sites it will check to see if
 * 					Any of the sites have been updated since last viewed. 
 * 
 */

import com.example.segment_saver.R.string;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.example.segment_saver.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.*;
import android.content.*;

import android.app.ProgressDialog;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

	private UpdateSites update_sites_task;
	private Settings settings_task;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item);
        adapter.add("No Updated Sites");
        
        ListView filters = (ListView) findViewById(R.id.sites);
        filters.setAdapter(adapter);
        
        Button get_button = (Button) findViewById(R.id.update_sites);
        Button settings_button = (Button) findViewById(R.id.settings);
        
        get_button.setOnClickListener(this);
        settings_button.setOnClickListener(this);
        
    }
    
    public void onClick(View v){
    	switch(v.getId())
    	{
    	case	R.id.update_sites:
    		update_sites_task = new UpdateSites();
    		update_sites_task.execute();
    			break;
    	case	R.id.settings:
    		Intent intent = new Intent(this, Delete_Site.class);
    		startActivity(intent);
    		//settings_task = new Settings();
    		//settings_task.execute();
    			// Can add more if we have a different button like change the update sites
    	}
    }
    
    private class Settings extends AsyncTask<Object, String, Boolean>
    {
    	private ProgressDialog progressDialog;
    	@Override
    	protected void onPreExecute() {
    		// This is a Dialog Box that appears While it works. 
    		//progressDialog = ProgressDialog.show(MainActivity.this, "Checking Sites for Updates", "Updating Site List ...", true, true);
    	}
    	@Override
    	protected void onPostExecute(Boolean result) {
    		// This closes the Dialog Box that it opens. 
    		//progressDialog.dismiss();
      	}
    	@Override
    	protected Boolean doInBackground(Object... params)
    	{
    		String[] sites = new String[2];// This has to be allocated to the exact size of strings that we want
    		sites[0]= "Hello";
    		sites[1]= "World";
    	
    		publishProgress(sites);
    		
    		//*/
    		return true;
    	}
    	
    	protected void onProgressUpdate(String... values){
    		ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,R.layout.list_item);
    		for(String site : values){
    			adapter.add(site);
    		}
    		ListView sites = (ListView) MainActivity.this.findViewById(R.id.sites);
    		sites.setAdapter(adapter);
    	}   
    	
    }
    
    
    private class UpdateSites extends AsyncTask<Object, String, Boolean> {
    	private ProgressDialog progressDialog;
    	@Override
    	protected void onPreExecute() {
    		// This is a Dialog Box that appears While it works. 
    		progressDialog = ProgressDialog.show(MainActivity.this, "Checking Sites for Updates", "Updating Site List ...", true, true);
    	}
    	@Override
    	protected void onPostExecute(Boolean result) {
    		// This closes the Dialog Box that it opens. 
    		progressDialog.dismiss();
      	}
    	@Override
    	protected Boolean doInBackground(Object... params)
    	{
    		// TODO: THis is where we add Jacobs Code... 
    		// It should contact Jacobs code if not insert Jacobs code here. 
    		// Can look at Tweet one from Anderson
    		String[] sites = new String[100];// This has to be allocated to the exact size of strings that we want
    		sites[0]= "Hello";
    		sites[1]= "World";
    		for(int i=0; i< 100; i++)
    		{
    			sites[i]= "Testing";
    		}
    		publishProgress(sites);
    		
    		
    		return true;
    	}
    	
    	protected void onProgressUpdate(String... values){
    		ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,R.layout.list_item);
    		for(String site : values){
    			adapter.add(site);
    		}
    		ListView sites = (ListView) MainActivity.this.findViewById(R.id.sites);
    		sites.setAdapter(adapter);
    	}
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
